import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:dio/dio.dart';
//import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'package:vegeshop/widgets/report/index.dart';

class BuyerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _BuyerPageState();
  }
}

class _BuyerPageState extends State<BuyerPage> {
  var count = 0;
  var spacer = SizedBox(height: 10.0);
  var _value = 'UGC';
  var _teststyle = "SMOKE";
  var _testName = '';
  var _testStatus = 0;    //后台测试是否完成
  var _isTesting = 0;    //后台是否正在执行测试
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("BSTrobot"),
        backgroundColor: Colors.orange,
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Image.asset('assets/sea.jpg'),
          new Divider(
            height: 20,
          ),
          new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              DropdownButton(
                  value: _value,
                  items: [
                    DropdownMenuItem(child: Text('UGC'), value: 'UGC'),
                    DropdownMenuItem(child: Text('ASKBOB'), value: 'ASKBOB')
                  ],
                  onChanged: (value) {
                    setState(() {
                      _value = value;
                    });
                  }),
              DropdownButton(
                  value: _teststyle,
                  items: [
                    DropdownMenuItem(child: Text('冒烟'), value: 'SMOKE'),
                    DropdownMenuItem(child: Text('回归'), value: 'BACK')
                  ],
                  onChanged: (value) {
                    setState(() {
                      _teststyle = value;
                    });
                  }),
              new RaisedButton(
                  onPressed: () {
                    if (_isTesting == 0) {
                      _testStatus = 0;
                      _isTesting = 1;
                      startRun();
                    } else {}
                  },
                  child: new Text(
                    "开始测试",
                    style: new TextStyle(color: Colors.blue),
                  )),
            ],
          ),
          new Divider(
            height: 60,
          ),
          new RaisedButton(
              onPressed: () {
                if (_isTesting == 1) {
                   getStatus();
                if (_testStatus == 1) {
                  _isTesting = 0;
                  Navigator.push(
                      context,
                      new MaterialPageRoute(
                          builder: (context) => new NewPage(testName: _testName)));
                } else {                  
                    print("waiting");
                    showAlertDialog(context);
                  }                  
                } else {
                   print(_testName);
                  Navigator.push(
                      context,
                      new MaterialPageRoute(                       
                          builder: (context) => new NewPage(testName: _testName)));
                }
              },
              child: new Text(
                "查看结果",
                style: new TextStyle(color: Colors.blue),
              )),          
        ],
      ),
    );
  }

  void startRun() async {
    Response response1 = await Dio().post(
        "https://pabst-core-stg.pingan.com.cn:8002/api/api/bstrobot/run/");
    var data1 = jsonDecode(response1.toString());
    _testName = data1['name'];
    print('name: $_testName');
  }

  void getStatus() async {
    var url =
        "https://pabst-core-stg.pingan.com.cn:8002/api/api/bstrobot/report/" +
            _testName.toString();
    Response response2 = await Dio().post(url);
    var data = jsonDecode(response2.toString());
    _testStatus = data['status'];
    print(_testStatus);
  }

  showAlertDialog(BuildContext context) {
    //设置按钮
    Widget okButton = FlatButton(
      child: Text("OK"),
      onPressed: () {
        Navigator.pop(context);
      },
    );

    //设置对话框
    AlertDialog alert = AlertDialog(
      title: Text("提示"),
      content: Text("测试进行中"),
      actions: [
        okButton,
      ],
    );

    //显示对话框
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }
}
